#include "projectHeader.h"

void* keepAliveMsg(void* t)
{

	//	printf("INSIDE KEEPALIVE MSG THREAD.\n");
	
		
	

	while(1)
	{
		


		//Iterate to map of all connected socket. and send keepalive to each.
		pthread_mutex_lock(&connectedNodeListMutex);
		pthread_mutex_lock(&sockIndexMapMutex);
		map<int,int>:: iterator keepAliveIt;
		/*
		if(terminate)
		{
			pthread_exit(NULL);
		}
		*/
		for(keepAliveIt=sockFDToQIndexMap.begin();keepAliveIt!=sockFDToQIndexMap.end();keepAliveIt++)
		{	

			queueItem keepAliveMsgToBePushed;
			keepAliveMsgToBePushed.sockfd=-1;
			keepAliveMsgToBePushed.msgHeader.messageType=0xF8;
			keepAliveMsgToBePushed.msgHeader.ttlInHeader=1;

			unsigned char *buff1;
			char* msg = (char*)"KPAV";
			buff1=GetUOID((char *)nodeInstanceId,msg,buf, sizeof(buf));
			memcpy(keepAliveMsgToBePushed.msgHeader.UOID,buf,20);
//			printf("\nInside keepalive : UOID = \n");
//			printUOID(buf);
			int mapIndex=(*keepAliveIt).second;
			int socketDesc=(*keepAliveIt).first;
			//pthread_mutex_lock(&keepAliveMapMutex);

				map<int,int>:: iterator temp;
				temp=keepAliveTimers.find(mapIndex);
				int timer=(*temp).second;

				if(timer!=0)
				{
					//pthread_mutex_unlock(&keepAliveMapMutex);
					pthread_mutex_lock(&wrtieQMutex[mapIndex]);

							wrtieThreadQueue[mapIndex].push_back(keepAliveMsgToBePushed);
							//printf("KEEP ALIVE THREAD : KEEP ALIVE MSG: Msg pushed to writeQueue of %d\n",mapIndex);
							pthread_cond_broadcast(&wrtieQCV[mapIndex]);

					pthread_mutex_unlock(&wrtieQMutex[mapIndex]);

						uint16_t portTemp=0;
							//uint16_t portTemp=0;
						char *tempHost=(char*)malloc(512);
							map<string,int>::iterator it;
							for(it=connectedNodeList.begin() ; it != connectedNodeList.end(); it++ )
							{
								if((*it).second==socketDesc)
								{
											char* str1 = (char*) malloc(512); 
											memset(&str1[0],'\0',512);
											memcpy(&str1[0],&(((*it).first).c_str()[0]),19);

											char *str=(char*)malloc(512);
											memset(str,'\0',512);
											memcpy(&str[0],&str1[0],strlen(str1));
											//printf("String from list is %s\n",str);
											
											tempHost=strtok(str,"_");
											//printf("TempHost is %s and length is %d\n",tempHost,(int)strlen(tempHost));
											char *tempPort=strtok(NULL,"_");
											//printf("TempPort is %s and length is %d\n",tempPort,(int)strlen(tempPort));
											
											portTemp=atoi(tempPort);
											break;
								}
							
							}
//						printf("\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");

						pthread_mutex_lock(&logFileMutex);

						gettimeofday(&tv, NULL); 
						int sec = tv.tv_sec ;
						int msec = tv.tv_usec/1000;
						char *time = (char *)malloc(14);
						memset(time,0,14);
						sprintf(time,"%10ld.%03d",(long)sec,msec);
//						printf("\nHHHHHHHHHHHHHHHHHHHHHHHH %d\n",strlen(time));
						time[strlen(time)]='\0';

						

//						printUOID(keepAliveMsgToBePushed.msgHeader.UOID);


						fprintf(logFilePtr, "s  %s %s:%hu KPAV 27 0x%02x %x%x%x%x \n",time,
							tempHost,
							portTemp,
							//(notifyMsgToBeSent.msgHeader.msgDataLength+27),
							(unsigned char)keepAliveMsgToBePushed.msgHeader.ttlInHeader,
							(unsigned char)keepAliveMsgToBePushed.msgHeader.UOID[16],
							(unsigned char)keepAliveMsgToBePushed.msgHeader.UOID[17],
							(unsigned char)keepAliveMsgToBePushed.msgHeader.UOID[18],
							(unsigned char)keepAliveMsgToBePushed.msgHeader.UOID[19]);
							//helloRecvData.HostPort,helloRecvData.Hostname);
						pthread_mutex_unlock(&logFileMutex);



















				}
				else
				{
					//pthread_mutex_unlock(&keepAliveMapMutex);
				}





		}
		pthread_mutex_unlock(&sockIndexMapMutex);
		pthread_mutex_unlock(&connectedNodeListMutex);
		usleep( (keepAliveTimeOut/2)*1000000);








		
		

	}//end of while(1)

	
	pthread_exit(NULL);
}
